<template>
  <div>
    <div class="main">
      <p>
        标题：<el-input
          v-model="article.title"
          placeholder="请输入内容"
          style="width: 600px"
        ></el-input>
      </p>
      <p>
        内容：<el-input
          type="textarea"
          :rows="16"
          placeholder="请输入内容"
          v-model="article.content"
        >
        </el-input>
      </p>
      <p>
        封面：<el-radio-group v-model="radio">
          <el-radio :label="3">单图</el-radio>
          <el-radio :label="6">三图</el-radio>
          <el-radio :label="9">无图</el-radio>
        </el-radio-group>
        <el-upload
          class="avatar-uploader"
          action="http://localhost:3000/addPic"
          :on-success="handleAvatarSuccess"
        >
          <img v-if="article.imgUrl" :src="article.imgUrl" class="avatar" />
          <i v-else class="el-icon-plus avatar-uploader-icon"></i>
        </el-upload>
      </p>
      <p>
        频道：<el-select v-model="article.typeid" placeholder="请选择">
          <el-option
            v-for="item in types"
            :key="item._id"
            :label="item.name"
            :value="item._id"
          >
          </el-option>
        </el-select>
      </p>
      <el-button @click="addArticle">添加</el-button>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      article: {
        title: "",
        content: "",
        imgUrl: "",
        typeid: "",
      },
      radio: 3,
      types:[],
    };
  },
  methods: {
    handleAvatarSuccess(res) {
      this.article.imgUrl = res.path;
    },
    async addArticle() {
        console.log(this.article);
      let {
        data: { code, msg },
      } = await this.axios.post("/addArticle", this.article);
      if (code == 200) {
        this.$message.success(msg);
        this.article = {
          title: "",
          textarea: "",
          imageUrl: "",
          typeid: "",
        };
      }
    },
    async getType(){
        let{data:{code,types}} = await this.axios.get("/getTypes")
        if(code==200){
            this.types = types
        }
    }
  },
  created(){
    this.getType()
  }
};
</script>

<style>
* {
  margin: 0;
  padding: 0;
}
p {
  margin: 20px 90px;
}
.avatar {
  width: 178px;
  height: 178px;
  display: block;
}
.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #409eff;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}
</style>